package com.t309.tzy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class FileUploadController {
    /**
     * 一定要是GetMapping
     * @return
     */
    @GetMapping("/toUpload")
    public String toUpload(){
        System.err.println("正在前往manager/upload/toUpload.jsp");
        //跳转视图
        return "manager/upload/toUpload";
    }
    /**
     * 接收多文件上传
     * @param files
     * @return
     * @throws IOException
     */
    @PostMapping("/doUpload")
    public String doUpload( MultipartFile[] files /*,
                            @RequestParam String name,
                            @RequestParam int age*/ ) throws IOException {
        System.err.println("doUpload()");
        for (MultipartFile file:files ) {
            final String fileName1 = file.getName();
            //System.out.println("size"+file.getSize());
            //获取 原始文件的名字
            String fileName = file.getOriginalFilename();//错误方式 : file.getName();
            System.out.println("fileName = "+fileName);
            //System.out.println("name="+ fileName.substring(fileName.lastIndexOf(".")) );
            String ext = fileName.substring(fileName.lastIndexOf("."));

            long curTime = System.currentTimeMillis();
            System.out.println("curTime = "+curTime);
            Path path = Paths.get("d:", "upload"+ext);
            System.out.println("path = "+path);
            //创建文件夹
            if(!Files.exists(path)){
                Files.createDirectory(path);
            }

            //String fileNewName = File.separator+"upload"+File.separator+curTime+ext;
            //file.transferTo(Paths.get("/","upload",fileName));
            //upload文件需要手动创建(代码创建 : file.mkdir())//最后那里是逗号,不是加号+
            file.transferTo(Paths.get("d:","upload",String.valueOf(curTime)+ext));;
        }

        //跳转视图
        return "WEB-INF/jsp/success";
    }

    /**
     * 接收文件上传
     * @param file
     * @return
     * @throws IOException
     */
    /*@PostMapping("/doUpload")
    public String doUpload(MultipartFile file) throws IOException {
        //System.out.println("size"+file.getSize());
        //获取 原始文件的名字
        String fileName = file.getOriginalFilename();//错误方式 : file.getName();
        System.out.println("fileName = "+fileName);
        //System.out.println("name="+ fileName.substring(fileName.lastIndexOf(".")) );
        String ext = fileName.substring(fileName.lastIndexOf("."));

        long curTime = System.currentTimeMillis();
        System.out.println("curTime = "+curTime);
        Path path = Paths.get("d:", "upload"+ext);
        System.out.println("path = "+path);
        //创建文件夹
        if(!Files.exists(path)){
            Files.createDirectory(path);
        }

        //String fileNewName = File.separator+"upload"+File.separator+curTime+ext;
        //file.transferTo(Paths.get("/","upload",fileName));
        //upload文件需要手动创建(代码创建 : file.mkdir())//最后那里是逗号,不是加号+
        file.transferTo(Paths.get("d:","upload",String.valueOf(curTime)+ext));;
        //跳转视图
        return "WEB-INF/jsp/success";
    }*/

}
